---
sidebar_class_name: node-only
---

# MongoDB Atlas

:::tip Compatibility
Only available on Node.js.
:::

LangChain.js supports MongoDB Atlas as a vector store, and supports both standard similarity search and maximal marginal relevance search,
which takes a combination of documents are most similar to the inputs, then reranks and optimizes for diversity.

## Setup

### Installation

First, add the Node MongoDB SDK to your project:

```bash npm2yarn
npm install -S mongodb
```

### Initial Cluster Configuration

Next, you'll need create a MongoDB Atlas cluster. Navigate to the [MongoDB Atlas website](https://www.mongodb.com/atlas/database) and create an account if you don't already have one.

Create and name a cluster when prompted, then find it under `Database`. Select `Collections` and create either a blank collection or one from the provided sample data.

** Note ** The cluster created must be MongoDB 7.0 or higher. If you are using a pre-7.0 version of MongoDB, you must use a version of langchainjs<=0.0.163.

### Creating an Index

After configuring your cluster, you'll need to create an index on the collection field you want to search over.

Switch to the `Atlas Search` tab and click `Create Search Index`. From there, make sure you select `Atlas Vector Search - JSON Editor`,
then select the appropriate database and collection and paste the following into the textbox:

```json
{
  "fields": [
    {
      "numDimensions": 1024,
      "path": "embedding",
      "similarity": "euclidean",
      "type": "vector"
    }
  ]
}
```

Note that the `dimensions` property should match the dimensionality of the embeddings you are using.
For example, Cohere embeddings have 1024 dimensions, and by default OpenAI embeddings have 1536:

**Note:** By default the vector store expects an index name of `default`, an indexed collection field name of `embedding`, and a raw text field name of `text`.
You should initialize the vector store with field names matching your index name collection schema as shown below.

Finally, proceed to build the index.

## Usage

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/community
```

### Ingestion

import CodeBlock from "@theme/CodeBlock";
import Ingestion from "@examples/indexes/vector_stores/mongodb_atlas_fromTexts.ts";

<CodeBlock language="typescript">{Ingestion}</CodeBlock>

### Search

import Search from "@examples/indexes/vector_stores/mongodb_atlas_search.ts";

<CodeBlock language="typescript">{Search}</CodeBlock>

### Maximal marginal relevance

import MMRExample from "@examples/indexes/vector_stores/mongodb_mmr.ts";

<CodeBlock language="typescript">{MMRExample}</CodeBlock>

### Metadata filtering

MongoDB Atlas supports pre-filtering of results on other fields. They require you to define which metadata fields
you plan to filter on by updating the index. Here's an example:

```json
{
  "fields": [
    {
      "numDimensions": 1024,
      "path": "embedding",
      "similarity": "euclidean",
      "type": "vector"
    },
    {
      "path": "docstore_document_id",
      "type": "filter"
    }
  ]
}
```

Above, the first item in `fields` is the vector index, and the second item is the metadata property you want to filter on.
The name of the property is `path`, so the above index would allow us to search on a metadata field named `docstore_document_id`.

Then, in your code you can use [MQL Query Operators](https://www.mongodb.com/docs/manual/reference/operator/query/) for filtering. Here's an example:

import MetadataExample from "@examples/indexes/vector_stores/mongodb_metadata_filtering.ts";

<CodeBlock language="typescript">{MetadataExample}</CodeBlock>
